import { Course } from "@/types/course";
import { Question, QuestionChapter, QuestionActionRecordWithId, QuestionActionRecordSource, QuestionChapterResp } from "@/types/practice";
import request from "@/utils/request";

const baseURL = "/train/question";

// 根据 set id 获取题目 id 列表
export const listQuestionIdBySetId = (questionSetId: string) =>
  request<Res<string[]>>({
    url: `${baseURL}/list-id-by-set-id`,
    method: "get",
    params: { questionSetId },
  });

/**
 * 根据课程id获取该课程下全部错题的章节分组（章节：train_question.chapter_id 关联 train_question_chapter.id
 * @param courseId 课程id
 * @returns 章节列表
 */
export const listWrongQuestionChapters = (courseId: Course["id"]) =>
  request<Res<QuestionChapterResp[]>>({
    url: `${baseURL}/list-member-error-chapter-by-course-id`,
    method: "get",
    params: { courseId },
  });

/**
 * 获取课程下指定章节的错题
 * @param courseId 课程id
 * @param chapterId 章节id（如果不传则加载全部
 * @returns 题目id列表
 */
export const listWrongQuestionIds = (courseId: Course["id"], chapterId?: QuestionChapter["id"]) =>
  request<Res<Question["id"][]>>({
    url: `${baseURL}/list-member-course-error-question-ids`,
    method: "post",
    data: { courseId, chapterId },
  });

/**
 * 根据课程id获取该课程下全部收藏的题目的章节分组
 * @param courseId 课程id
 * @returns 章节列表
 */
export const listCollectQuestionChapters = (courseId: Course["id"]) =>
  request<Res<QuestionChapterResp[]>>({
    url: `${baseURL}/list-member-collect-chapter-by-course-id`,
    method: "get",
    params: { courseId },
  });

/**
 * 获取课程下指定章节的收藏题目
 * @param courseId 课程id
 * @param chapterId 章节id
 * @returns 题目id列表
 */
export const listCollectQuestionIds = (courseId: Course["id"], chapterId?: QuestionChapter["id"]) =>
  request<Res<Question["id"][]>>({
    url: `${baseURL}/list-member-course-collect-question-ids`,
    method: "post",
    data: { courseId, chapterId },
  });

// 根据 tag id 获取题目 id 列表
export const listQuestionIdByTagId = (questionTagId: string) =>
  request<Res<string[]>>({
    url: `${baseURL}/list-id-by-tag-id`,
    method: "get",
    params: { questionTagId },
  });

// 根据题目 id 列表获取题目列表
export const listQuestionByIds = (questionIds: Question["id"][]) =>
  request<Res<Question[]>>({
    url: `${baseURL}/list-by-ids`,
    method: "post",
    data: questionIds,
  });

// 批量创建或更新用户行为记录
export const batchCreateOrUpdateMemberActionRecord = (
  /** 记录来源类型 */
  sourceFromType: QuestionActionRecordSource,
  /** 记录来源id */
  sourceFromId: string,
  /** 记录列表 */
  actionRecords: QuestionActionRecordWithId[]
) => {
  const data = [...actionRecords].map((item) => ({
    ...item,
    sourceFromType,
    sourceFromId,
  }));

  return request<Res<any>>({
    url: `${baseURL}/batch-create-member-action-record`,
    method: "post",
    data,
  });
};

// 获取当前用户全部行为记录
export const listMemberActionRecord = (
  /** 记录来源类型 */
  sourceFromType?: QuestionActionRecordSource,
  /** 记录来源id */
  sourceFromId?: string
) =>
  request<Res<QuestionActionRecordWithId[]>>({
    url: `${baseURL}/list-member-action-record`,
    method: "post",
    data: { sourceFromType, sourceFromId },
  });

// 根据课程获取收藏记录
export const listMemberCollectRecord = (
  /** 课程id */
  courseId: Course["id"]
) =>
  request<Res<Question["id"][]>>({
    url: `${baseURL}/list-member-collect-ids-by-course-id`,
    method: "get",
    params: { courseId },
  });

export type CollectRecordInfo = { questionId: Question["id"]; ifCollect: boolean };

export const createOrUpdateMemberCourseCollect = (courseId: Course["id"], collectRecordInfos: CollectRecordInfo[]) =>
  request<Res<any>>({
    url: `${baseURL}/create-or-update-member-course-collect`,
    method: "post",
    data: { courseId, collectRecordInfos },
  });

// 传入课程id、题目id，更新题目收藏状态，返回是否更新成功
export const updateCollectRecordStatus = (
  /** 课程id */
  courseId: string,
  /** 待更新的收藏状态表 */
  statusMap: Record<Question["id"], boolean>
) =>
  request<Res<boolean>>({
    url: `${baseURL}/update-collect-record-status`,
    method: "post",
    data: { courseId, statusMap },
  });

export const listChapterByIds = (chapterIds: Question["chapterId"][]) =>
  request<Res<QuestionChapter[]>>({
    url: `${baseURL}/list-chapter-by-ids`,
    method: "post",
    data: chapterIds,
  });

export const listChapterByQuestionIds = (questionIds: Question["id"][]) =>
  request<Res<QuestionChapter[]>>({
    url: `${baseURL}/list-chapter-by-question-ids`,
    method: "post",
    data: questionIds,
  });

export const initPersonalPaper = (carType: Question["carType"], subject: Question["subject"]) =>
  request<Res<string>>({
    url: `${baseURL}/init-personal-paper`,
    method: "post",
    data: { carType, subject },
  });

export const listMemberPaperIds = (carType: Question["carType"], subject: Question["subject"]) =>
  request<Res<string[]>>({
    url: `${baseURL}/list-member-paper-ids`,
    method: "post",
    data: { carType, subject },
  });
